//
//  FriendViewController.m
//  BGM
//
//  Created by liuqin on 2018/11/29.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "FriendViewController.h"
#import "Common.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "UIImageView+WebCache.h"
#import "MePhotosViewController.h"
#import "MyPushViewController.h"
#import "BGMFriendPhotoTableViewCell.h"
#import "BGMFriendPhotoTableViewCell.h"
#import "ChatViewController.h"
#import <NIMSDK/NIMSDK.h>
#import "SVProgressHUD.h"


@interface FriendViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UIView *bgView;

@property (nonatomic,strong)UIImageView *headImage;
@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)UIButton *sexBtn;
@property (nonatomic,strong)UILabel *cityLabel;
@property (nonatomic,strong)UILabel *zhuanyeLabel;
@property (nonatomic,strong)UILabel *vauleLabel;


@property (nonatomic,strong)NSDictionary *userInfo;
@property (nonatomic,strong)UITableView *userTable;

@property (nonatomic,strong)UIButton *bottomBtn;


@end

@implementation FriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topBackView.hidden = NO;
    float imageW = SCREENW/3-20;
    self.headImage = [[UIImageView alloc]initWithFrame:CGRectMake((SCREENW-imageW)/2, self.navview.frame.size.height, imageW, imageW)];
    [self.headImage setImage:[UIImage imageNamed:@"headImage"]];
    self.headImage.layer.cornerRadius =imageW/2;
    self.headImage.layer.masksToBounds = YES;
    [self.view addSubview:self.headImage];
    
    float y = self.headImage.frame.origin.y+SCREENW/4/2;
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(15,y, SCREENW-30, SCREENH-y-20)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self.view insertSubview:bgView belowSubview:self.headImage];
    [self getInfo];
    
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.headImage.frame.size.height+self.headImage.frame.origin.y, SCREENW, 40)];
    self.nameLabel.backgroundColor = [UIColor clearColor];
     self.nameLabel.textColor = bgmmainColor;
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    self.nameLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:self.nameLabel];
    
    float x = (SCREENW-3*60)/2;
    
    self.sexBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, self.nameLabel.frame.origin.y+self.nameLabel.frame.size.height, 50, 20)];
    self.sexBtn.layer.masksToBounds = YES;
    self.sexBtn.layer.cornerRadius = 10;
    self.sexBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.sexBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:self.sexBtn];
    
    self.cityLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.sexBtn.frame.origin.x+self.sexBtn.frame.size.width+10, self.sexBtn.frame.origin.y, 50, 20)];
    self.cityLabel.textColor = bgmgrayColor;
    self.cityLabel.font = [UIFont systemFontOfSize:11];
    self.cityLabel.backgroundColor = bgmColor(250, 241, 194);
    self.cityLabel.layer.masksToBounds = YES;
    self.cityLabel.layer.cornerRadius = 10;
    self.cityLabel.textAlignment = NSTextAlignmentCenter;
    self.cityLabel.layer.borderColor = [bgmColor(250, 223, 93) CGColor];
    self.cityLabel.layer.borderWidth = 1;
    [self.view addSubview:self.cityLabel];
    
    self.zhuanyeLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.cityLabel.frame.origin.x+self.cityLabel.frame.size.width+10, self.sexBtn.frame.origin.y, 50, 20)];
    self.zhuanyeLabel.textColor = bgmgrayColor;
    self.zhuanyeLabel.font = [UIFont systemFontOfSize:11];
    self.zhuanyeLabel.backgroundColor = bgmColor(250, 241, 194);
    self.zhuanyeLabel.layer.masksToBounds = YES;
    self.zhuanyeLabel.layer.cornerRadius = 10;
    self.zhuanyeLabel.textAlignment = NSTextAlignmentCenter;
    self.zhuanyeLabel.layer.borderColor = [bgmColor(250, 223, 93) CGColor];
    self.zhuanyeLabel.layer.borderWidth = 1;
    [self.view addSubview:self.zhuanyeLabel];
    
    
    UIImageView *iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(self.sexBtn.frame.origin.x+10, self.sexBtn.frame.origin.y+self.sexBtn.frame.size.height+5, 24, 25)];
    [iconImage setImage:[UIImage imageNamed:@"meValues1"]];
    [self.view addSubview:iconImage];

    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(self.zhuanyeLabel.frame.origin.x-10, iconImage.frame.origin.y, 50, 35)];
    label1.text = @"靠谱值";
    label1.textColor = bgmgrayColor;
    label1.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:label1];
    
    
    self.vauleLabel = [[UILabel alloc]initWithFrame:CGRectMake(iconImage.frame.origin.x+iconImage.frame.size.width+10,iconImage.frame.origin.y, label1.frame.origin.x-(iconImage.frame.origin.x+iconImage.frame.size.height), 35)];
     self.vauleLabel.font = [UIFont boldSystemFontOfSize:17];
     self.vauleLabel.textAlignment = NSTextAlignmentCenter;
    self.vauleLabel.text = @"235";
    [self.view addSubview:self.vauleLabel];
    
//
    self.userTable = [[UITableView alloc]initWithFrame:CGRectMake(30, SCREENH>736?300:270, SCREENW-60, SCREENH-270-100)];
    self.userTable.delegate = self;
    self.userTable.dataSource = self;
    self.userTable.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.userTable];
    [self.userTable setScrollEnabled:NO];
    [self setExtraCellLineHidden:self.userTable];
    
    self.bottomBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 220, 45)];
    self.bottomBtn.layer.cornerRadius = 22;
    self.bottomBtn.layer.masksToBounds = YES;
    self.bottomBtn.backgroundColor = bgmyelloeColor;
    [self.bottomBtn setTitleColor:bgmgrayColor forState:UIControlStateNormal];
    [self.view addSubview:self.bottomBtn];
    self.bottomBtn.center = CGPointMake(SCREENW/2, SCREENH-60);
    [self.bottomBtn addTarget:self action:@selector(clickMessage) forControlEvents:UIControlEventTouchUpInside];
    
    self.bottomBtn.hidden = YES;
    
}

-(void)setExtraCellLineHidden: (UITableView *)tableView

{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
         id imageArray = [self.userInfo objectForKey:@"image"];
        if ([imageArray isKindOfClass:[NSArray class]]) {
            NSArray *array = imageArray;
            if (array.count>0) {
                return 80+55+20;
            }
        }
        return 55;
    }else if(indexPath.row==1){
        return 55;
    }
    return 60;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 0) {
        static NSString *identifier = @"BGMFriendPhotoTableViewCell";
        BGMFriendPhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[BGMFriendPhotoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        id imageArray = [self.userInfo objectForKey:@"image"];
        if ([imageArray isKindOfClass:[NSArray class]]) {
            NSArray *array = imageArray;
            if (array.count>0) {
                [cell setUserPhoto:imageArray];
            }
        }
        
        
        return cell;
    }
    else if (indexPath.row ==1){
        static NSString *identifier = @"BGMFriendPhotoTableViewCell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREENW-15, 0.3)];
            label.backgroundColor = [UIColor grayColor];
            label.alpha = 0.3;
            [cell addSubview:label];
            
            UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 80, 55)];
            label1.text = @"求助记录";
            label1.textColor = bgmmainColor;
            [cell addSubview:label1];
            
            UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENW-30-250, 0, SCREENW-(SCREENW-50-250)-110, 55)];
            rightLabel.tag = 100;
            rightLabel.textColor = bgmgrayColor;
            
            rightLabel.textAlignment = NSTextAlignmentRight;
            rightLabel.font = [UIFont systemFontOfSize:14];
            [cell addSubview:rightLabel];
            
            UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-60-50, 0, 55, 55)];
            [rightBtn setImage:[UIImage imageNamed:@"gengduo"] forState:UIControlStateNormal];
            [cell addSubview:rightBtn];
            
        }
        
        UILabel *label = (UILabel *)[cell viewWithTag:100];
        NSString *publishTitle = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"publishTitle"]];
        if (publishTitle.length>0) {
            label.text = publishTitle;
        }else{
            label.text = @"还没有发布过帮忙信息";
        }
        
        return cell;
        
    }else{
        static NSString *identifer = @"BGMFriendPhotoTableViewCell2";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREENW-15, 0.3)];
            label.backgroundColor = [UIColor grayColor];
            label.alpha = 0.3;
            [cell addSubview:label];
            
            UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 80, 55)];
            label1.text = @"个人标签";
            label1.textColor = bgmmainColor;
            [cell addSubview:label1];
            
            UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(15, 35, SCREENW-60, 55)];
            label2.textColor = bgmgrayColor;
            label2.textAlignment = NSTextAlignmentCenter;
            label2.font = [UIFont systemFontOfSize:12];
            label2.tag = 100;
            [cell addSubview:label2];
            
        }
        UILabel *label = (UILabel *)[cell viewWithTag:100];
        NSArray *array = [self.userInfo objectForKey:@"personalLabel"];
        if (array.count>0) {
            NSMutableString *mutableStr = [[NSMutableString alloc]init];
            for (int i = 0; i<array.count; i++) {
                NSString *str = [array objectAtIndex:i];
                [mutableStr appendString:[NSString stringWithFormat:@"%@、",str]];
            }
            label.text = mutableStr;
        }else{
            label.text = @" \"或许他只想做个简简单单的好人吧！\" ";
        }
        
        return cell;
    }
    
    
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        
        MePhotosViewController *vc = [[MePhotosViewController alloc]init];
        vc.isOther = YES;
        id array = [self.userInfo objectForKey:@"image"];
        if ([array isKindOfClass:[NSArray class]]) {
            vc.imageArray = array;
        }else{
            vc.imageArray = [[NSMutableArray alloc]init];
        }
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row ==1){
        
        NSString *publishTitle = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"publishTitle"]];
        if (publishTitle.length>0) {
            MyPushViewController *vc = [[MyPushViewController alloc]init];
            vc.isOther = YES;
            vc.userId = self.chUsersId;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
        }
        
        
       
    }
}
-(void)getInfo{
    NSDictionary *params = @{@"userToken":self.userToken,@"userId":self.userId,@"chUsersId":self.chUsersId};
    [BGMHTTP requestWithMethodName:@"businesscard/getUsersCards" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
            if ([code isEqualToString:@"200"]) {
                self.userInfo = [result objectForKey:@"data"];
                [self reloadTableView];
            }
        });
     
    }];
    
}
- (void)reloadTableView{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.userTable  reloadData];
        NSString *url = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"headImage"]];
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"headImage"]];
        self.nameLabel.text = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"name"]];
        NSString *sex = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"gender"]];
        sex = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"birthday"]];
        
        [self.sexBtn setTitle:sex forState:UIControlStateNormal];
        
        if ([sex isEqualToString:@"1"]) {//男
            self.sexBtn.backgroundColor = bgmColor(68, 209, 208);
            [self.sexBtn setImage:[UIImage imageNamed:@"sex_1"] forState:UIControlStateNormal];
        }else{
            self.sexBtn.backgroundColor = bgmColor(251, 119, 123);
            [self.sexBtn setImage:[UIImage imageNamed:@"sex_2"] forState:UIControlStateNormal];

        }
        self.cityLabel.text =[NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"city"]];
        sex = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"industry"]];
        if (sex.length>0) {
            self.zhuanyeLabel.text = sex;
        }else{
            self.zhuanyeLabel.text = @"自由";
        }
        sex = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"spectralValue"]];
        self.vauleLabel.text = sex;
        
        
        if (self.ftype == UserFriend) {
            self.bottomBtn.hidden = NO;
            [self.bottomBtn setTitle:@"发消息" forState:UIControlStateNormal];
        }else if(self.ftype == UserFind){
           NSString *isitFriends = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"isitFriends"]];
            if ([isitFriends isEqualToString:@"true"]) {
                self.bottomBtn.hidden = NO;
                [self.bottomBtn setTitle:@"发消息" forState:UIControlStateNormal];
            }else{
                self.bottomBtn.hidden = YES;
            }
            
        }else if (self.ftype == ChatFind){
            self.bottomBtn.hidden = NO;
            NSString *isitFriends = [NSString stringWithFormat:@"%@",[self.userInfo objectForKey:@"isitFriends"]];
            if ([isitFriends isEqualToString:@"true"]) {
                [self.bottomBtn setTitle:@"发消息" forState:UIControlStateNormal];
            }else{
                [self.bottomBtn setTitle:@"加好友" forState:UIControlStateNormal];
            }
        }
        
    });
    
}
- (void)clickMessage{
    
    if ([self.bottomBtn.titleLabel.text isEqualToString:@"发消息"]) {
        ChatViewController *vc = [[ChatViewController alloc]init];
        vc.otherName = self.otherName;
        vc.OtherId = self.chUsersId;
        vc.otherImage = self.otherImage;
        NIMSession *session = [NIMSession session:self.chUsersId type:NIMSessionTypeP2P];
        vc.historysession = session;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([self.bottomBtn.titleLabel.text isEqualToString:@"加好友"]){
        NSDictionary *param ;
        if(self.isRootGroupFrom){
            param = @{@"userToken":[BGMUser shareInstance].userToken,@"userId":[BGMUser shareInstance].userId,@"friendId":self.chUsersId,@"groupId":self.groupId,@"type":@"2"};
        }else{
             param = @{@"userToken":[BGMUser shareInstance].userToken,@"userId":[BGMUser shareInstance].userId,@"friendId":self.chUsersId,@"type":@"1"};
        }
        [BGMHTTP requestWithMethodName:@"businesscard/addFriends" parameters:param completion:^(BOOL success, NSError *error, NSDictionary *result) {
            NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
            if ([code isEqualToString:@"200"]) {
                [SVProgressHUD showSuccessWithStatus:@"请求加好友成功,请耐心等待~"];
            }
        }];
       
        
    }else{
        
    }
    
    
   
    
}


@end
